<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container justify-between">
      <div>
        <el-switch v-model="multi" inactive-text="批量操作"></el-switch>
        <template v-if="multi">
          <el-button size="mini" type="info" @click="doExportExcel()">导出花名册</el-button>
          <el-button size="mini" type="primary" @click="noticeMianshi()">通知面试</el-button>
        </template>
      </div>
      <div class="">
        <el-select @change='doSearch' style="width: 150px" class="filter-item" v-model="listQuery.job_id"
                   placeholder="全部岗位">
          <el-option label="全部岗位" value=""></el-option>
          <el-option v-for="(item,index) in job_list" :key="item.id" :label="item.name"
                     :value="item.id"></el-option>
        </el-select>
        <el-select @change='doSearch' style="width: 150px" class="filter-item" v-model="listQuery.work_place"
                   placeholder="全部地址">
          <el-option label="全部地址" value=""></el-option>
          <el-option v-for="(item,index) in place_list" :key="item" :label="item"
                     :value="item"></el-option>
        </el-select>
        <el-select @change='doSearch' style="width:120px" class="filter-item" v-model="listQuery.gender"
                   placeholder="全部性别">
          <el-option  label="全部性别" value=""></el-option>
          <el-option v-for="(item,index) in gender_list" :key="item.data_value" :label="item.data_text"
                     :value="item.data_value"></el-option>
        </el-select>
        <el-input @keyup.enter.native="doSearch" style="width: 120px;" class="filter-item" placeholder="最小年龄"
                  v-model="listQuery.start_age">
        </el-input>
        <el-input @keyup.enter.native="doSearch" style="width: 120px;" class="filter-item" placeholder="最大年龄"
                  v-model="listQuery.end_age">
        </el-input>
        <el-input @keyup.enter.native="doSearch" style="width: 120px;" class="filter-item" placeholder="输入名称搜索"
                  v-model="listQuery.keyword">
        </el-input>
        <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="doSearch">搜索
        </el-button>
      </div>
    </div>
    <el-table ref="multipleTable" :data="list" v-loading="listLoading" element-loading-text="给我一点时间"
              border fit highlight-current-row style="width: 100%" @selection-change="onSelectionChange"
              height="600">
      <el-table-column v-if='multi' type="selection" width="55"></el-table-column>
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column align="left" label="岗位" width="200px">
        <template slot-scope="scope">
          <span>【{{scope.row.dept_name}}】{{(scope.row.job_name == "通用岗位") ? "---" : scope.row.job_name}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="应聘人" width="80">
        <template slot-scope="scope">
                    <span class="relative_span">{{scope.row.name}} <i class='position_black'
                                                                      v-if="scope.row.black_flag==1">黑</i></span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="手机号" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="性别" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.gender | show_basedatas('gender', '---')}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label=" 年龄" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.birthday | showFixedAge('---')}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label=" 登记表" width="130">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="scanResume(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column align="left" label="工作地点" >
        <template slot-scope="scope">
          <span>{{scope.row.work_place}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="离职时间" >
        <template slot-scope="scope">
          <span>{{scope.row.lizhi_time | beautiful_time}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="离职原因" >
        <template slot-scope="scope">
          <span>{{scope.row.lizhi_reason}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="离职处理时间" >
        <template slot-scope="scope">
          <span>{{scope.row.update_time | beautiful_time}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="操作" class-name="small-padding fixed-width" fixed="right" width="350">
        <template slot-scope="scope">
          <el-button round size="mini" type="primary" @click="rollReturn(scope.row.id)">回退</el-button>
          <el-button round size="mini" type="info" @click="printResume(scope.row.id)">打印</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="onSizeChange" @current-change="onCurrentChange"
                     :current-page="listQuery.pageNumber" :page-sizes="[12,50,100,10000]"
                     :page-size="listQuery.pageSize"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>



    <mianshiWin @refreshList="loadList" @noticeWinClose="noticeWinClose" v-if="showMianshi" :ids="ids"
                :mobiles="mobiles"></mianshiWin>
    <resumeTemplate v-if="showResumeTemplate" :id="resume_dengji_id" :template="template"
                    @resumeWinClose="resumeWinClose"></resumeTemplate>
    <resumePrint :template="template" :bean_arr="bean_arr" v-if="showPrint"
                 @printWinClose="printWinClose"></resumePrint>
    <interviewWin @interviewWinClose="interviewWinClose" v-if="showInterview"
                  :interview_data="interview_data"></interviewWin>
  </div>
</template>
<script>
	import Vue from 'vue'
	import waves from '@/directive/waves'  // 水波纹指令
	import API from '@/api'
	import Http from '@/http'
	import Store from '@/store'
	import Msg from '@/utils/msg'
	import Utils from '@/utils/common'
	import _ from 'underscore'
	import Qiniu from '@/utils/qiniu'
	import {mapGetters} from 'vuex'
	import mianshiWin from '@/components/mianshiWin'
	import resumeTemplate from '@/components/resumeTemplate'
	import interviewWin from '@/components/interviewWin'
	import resumePrint from '@/components/resumePrint'

	export default {
		directives: {waves},
		data() {
			return {
				dialogTitle: '填写离职原因',
				dialogFormVisible: false,
				lizhiBean:{},
				formRules: {
					lizhi_reason: [{required: true, message: '请填写离职原因', trigger: 'blur'}],
					lizhi_time: [{required: true, message: '请选择离职时间', trigger: 'blur'}]
				},
				dealing: false,
				multi: true,
				company: Store.getters.user.company,
				list: [],
				total: 0,
				listLoading: true,
				listQuery: {
					pageNumber: 1,
					pageSize: 12,
					keyword: '',
					status: 6,
					job_id: '',
					gender:'',
					start_age:'',
					end_age:'',
				},
				job_list: [],
				place_list:[],
				status_list: Store.getters.basedata.dengji_status,
				gender_list: Store.getters.basedata.gender,
				interview_step: Store.getters.basedata.interview_step,
				bean: {},
				multipleSelection: [],
				showMianshi: false,
				mobiles: '',
				ids: '',
				showResumeTemplate: false,
				resume_dengji_id: '',
				template: '',
				resumeData: [],
				showInterview: false,
				showPrint: false,
				bean_arr: []
			}
		},
		filters: {},
		components: {
			mianshiWin, resumeTemplate, interviewWin, resumePrint
		},
		created() {
			this.loadList()
			this.loadPlaceList()
			this.loadJobList()
		},
		computed: {},
		methods: {
			loadJobList() {
				Http.getModel(API.get_company_job_list, {}, false).then((json) => {
					this.loading = false
					if (json.code == 0) {
						this.job_list = json.data.list
					}
				})
			},
			loadList() {
				this.listLoading = true
				Http.getModel(API.get_dengji_list, this.listQuery, false).then((json) => {
					this.loading = false
					if (json.code == 0) {
						var list = json.data.list
						for (let i = 0; i < list.length; i++) {
							if (list[i].interviews) {
								list[i].interview_bean = []
								var str2 = list[i].interviews;
								var str1 = list[i].interview_detail;
								if (str2 && str2 != 'null') {
									var arr = str2.split(',')
									var arr_detail = JSON.parse(str1) || [];
									var return_arr = [];
									for (let i = 0; i < arr.length; i++) {
										if (arr_detail[i]) {
											return_arr.push(arr_detail[i])
										} else {
											var item = {
												interview_id: arr[i],
												result: 0,
												step: i + 1
											}
											return_arr.push(item)
										}
									}
									list[i].interview_bean = return_arr
								}
							}
						}
						this.list = list
						this.total = parseInt(json.data.total)
						this.listLoading = false
					}
				})
			},
			loadPlaceList() {
				Http.getModel(API.get_work_place_list, {}, false).then((json) => {
					this.loading = false
					if (json.code == 0) {
						this.place_list = json.data.list
					}
				})
			},
			rollReturn:function(id){
				Msg.confirm("确认回退吗？",() =>{
					Http.getModel(API.rollback, {id: id,status:6}, false).then((json) => {
						this.loading = false
						if (json.code == 0) {
							this.loadList()
						}
					})
				});
			},
			/***   公用部分 start ***/
			onSelectionChange(val) {
				this.multipleSelection = val;
			}
			,
			doSearch() {
				this.listQuery.pageNumber = 1
				this.loadList()
			}
			,
			onSizeChange(val) {
				this.listQuery.pageSize = val
				this.loadList()
			}
			,
			onCurrentChange(val) {
				this.listQuery.pageNumber = val
				this.loadList()
			},
			/***   公用部分 end ***/
			// 查看登记表
			scanResume(id) {
				this.showResumeTemplate = true
				this.resume_dengji_id = id
				var company = this.company;
				var template_id = '';
				if (company.template) {
					template_id = company.template
				} else {
					template_id = 'B88888888'
				}
				this.template = template_id
			},
			resumeWinClose() {
				this.showResumeTemplate = false
			},
			// 通知面试
			noticeMianshi() {
				var mobiles = '';
				var ids = "";
				if (this.multipleSelection.length == 0) {
					Msg.error("请先选择");
					return false;
				} else {
					ids = _.pluck(this.multipleSelection, "id").join(",");
					mobiles = _.pluck(this.multipleSelection, "mobile").join(",");
				}
				this.ids = ids;
				this.mobiles = mobiles
				this.showMianshi = true
			},
			noticeWinClose() {
				this.showMianshi = false
			},
			//  导出花名册
			doExportExcel: function (ids) {
				if (!ids) {
					if (this.multipleSelection.length == 0) {
						Msg.error("请先选择");
						return false;
					} else {
						ids = _.pluck(this.multipleSelection, "id").join(",");
					}
				}
				var company = this.company;
				var template_id = '';
				if (company.template) {
					template_id = company.template
				} else {
					template_id = 'B88888888'
				}
				var DownLoadFile = function (options) {
					var config = $.extend(true, {method: 'post'}, options);
					var $iframe = $('<iframe id="down-file-iframe" />');
					var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
					$form.attr('action', config.url);
					for (var key in config.data) {
						$form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
					}
					$iframe.append($form);
					$(document.body).append($iframe);
					$form[0].submit();
					$iframe.remove();
				}
				var params = {
					url: window.server_root + API.export_worker_excel,
					data: {
						ids: ids,
						template: template_id
					}
				}
				DownLoadFile(params)
				Http.getModel(API.set_huamingce_flag, {ids: ids}, false).then((json) => {
					this.loading = false
					if (json.code == 0) {
						this.loadList()
					}
				})
			},
			//  打印简历
			printWinClose() {
				this.showPrint = false
			},
			printResume(ids) {
				if (!ids) {
					if (this.multipleSelection.length == 0) {
						Msg.error("请先选择");
						return false;
					} else {
						ids = _.pluck(this.multipleSelection, "id").join(",");
					}
				}
				var template_id = '';
				if (this.company.template) {
					template_id = this.company.template
				} else {
					template_id = 'B88888888'
				}
				this.template = template_id
				this.ids = ids
				this.loadResume()
			},
			loadResume() {
				Http.getModel(API.get_dengji_list_by_ids, {ids: this.ids}, false).then((json) => {
					this.dealing = false
					if (json.code == 0) {
						var bean_arr = json.data.list;
						bean_arr.forEach((bean) => {
							var company_logo = this.company.logo;
							if (bean.ext_info) {
								bean.ext_info_obj = JSON.parse(bean.ext_info);
								console.log("ext_info_obj", bean.ext_info_obj.family_situation)
							} else {
								bean.ext_info_obj = {
									family_situation: [],
									work_experience: [],
									study_experience: []
								};
							}
							if (company_logo) {
								bean.company_logo = company_logo;
							}
							if (bean.avatar) {
								bean.avatar_url = this.decodeImg(bean.avatar)
							}
							var template = this.template
							if (template == 'B88888888') {
								bean.ext_info_obj.family_situation = this.decodeStr(bean.ext_info_obj.family_situation, 3)
								bean.ext_info_obj.work_experience = this.decodeStr(bean.ext_info_obj.work_experience, 3)
								bean.ext_info_obj.study_experience = this.decodeStr(bean.ext_info_obj.study_experience, 3)
							}
							if (template == 'A00010011') {
								bean.ext_info_obj.work_experience = this.decodeStr(bean.ext_info_obj.work_experience, 1)
								bean.ext_info_obj.study_experience = this.decodeStr(bean.ext_info_obj.study_experience, 2)
							}
							if (template == 'A00010009') {
								bean.ext_info_obj.family_situation = this.decodeStr(bean.ext_info_obj.family_situation, 2)
								bean.ext_info_obj.work_experience = this.decodeStr(bean.ext_info_obj.work_experience, 1)
							}
						})
						this.bean_arr = bean_arr
						this.showPrint = true
						Http.getModel(API.set_print_flag, {ids: this.ids}, false).then((json) => {
							this.loading = false
							if (json.code == 0) {
								this.loadList()
							}
						})

					} else {
						Msg.error(json.msg)
					}
				})
			},
			decodeImg(value) {
				if (value) {
					var value = value;
					if (!Utils.startWith("http", value)) {
						if (value.indexOf(",") > -1) {
							return value
						} else {
							return "data:image/jpeg;base64," + value
						}
					}
					return value;
				} else {
					return ''
				}
			},
			decodeStr(str, length) {
				if (typeof(str) == 'object') {
					var obj_arr = str || [];
					console.log("obj_arr:", obj_arr);
					var obj_arr_2 = [];
					if (obj_arr.length <= length) {
						for (var i = 0; i < length; i++) {
							if (obj_arr[i]) {
								obj_arr_2[i] = obj_arr[i];
							} else {
								obj_arr_2[i] = [];
							}
						}
					} else {
						obj_arr_2 = obj_arr
					}
					return obj_arr_2;
				}
				if (typeof(str) == 'string') {
					console.log("str:", str)
					var str_arr = (str && str.split("|")) || [];
					var obj_arr = [];
					var obj_arr_2 = [];
					for (var i = 0; i < str_arr.length; i++) {
						var text_arr = str_arr[i].split(",");
						obj_arr[i] = [];
						for (var j = 0; j < text_arr.length; j++) {
							obj_arr[i][j] = text_arr[j]
						}
					}
					if (obj_arr.length <= length) {
						var obj_arr_2 = [];
						for (var i = 0; i < length; i++) {
							if (obj_arr[i]) {
								obj_arr_2[i] = obj_arr[i];
							} else {
								obj_arr_2[i] = [];
							}
						}
					} else {
						obj_arr_2 = obj_arr
					}
					return obj_arr_2;
				}
			},
			//  下载简历
			downloadResume(id){
				var company = this.company;
				var template_id = '';
				if(company.template){
					template_id = company.template
				}else{
					template_id = 'B88888888'
				}
				var DownLoadFile = function (options) {
					var config = $.extend(true, { method: 'post' }, options);
					var $iframe = $('<iframe id="down-file-iframe" />');
					var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
					$form.attr('action', config.url);
					for (var key in config.data) {
						$form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
					}
					$iframe.append($form);
					$(document.body).append($iframe);
					$form[0].submit();
					$iframe.remove();
				}
				var params = {
					url:window.server_root+API.batch_export_resumes,
					data:{
						ids:id,
						template:template_id
					}
				}
				DownLoadFile(params)
			},
			//  显示面试情况
			showInterviewWin(id, str) {
				this.showInterview = true
				var interview_id = id;
				var detail_array = JSON.parse(str);
				var detail_obj = {};
				for (var i = 0; i < detail_array.length; i++) {
					if (detail_array[i].interview_id == interview_id) {
						detail_obj = detail_array[i];
						break
					}
				}
				this.interview_data = detail_obj
			},
			interviewWinClose() {
				this.showInterview = false
			},


		}
	}
</script>
<style>

</style>
